<template>
		<div class="rolebox">
			<el-row>
				<el-tabs type="border-card" v-model="activeName">
					<el-tab-pane label="所有权限" name="0">
						<div>
							<el-button type="primary" style="float:left" @click="add">新增</el-button>
						</div>
						<el-table :data="rights">
							<el-table-column prop="rid" label="id"></el-table-column>
							<el-table-column prop="rname" label="权限名"></el-table-column>
							<el-table-column prop="num" label="人数"></el-table-column>
							<el-table-column prop="deptno" label="部门编号"type="hidden"></el-table-column>
							<el-table-column label="操作">
								<template slot-scope="s">
						<!-- 利用作用域插槽，将当前行作为参数传给了方法del -->
						<!-- s.$index可以获取当前行数据的索引 -->
									<el-button type="success" @click="addemp(s.$index,s.row)">添加员工权限</el-button>
								</template>
							</el-table-column>
						</el-table>
		
						<el-dialog title="新增权限" :visible.sync="dialogFormVisible">
							<el-form :model="form" label-width="100px">
								<el-form-item label="权限名称:">
									<el-input v-model="form.rname" autocoplete="off" @change="checkpick3()"></el-input>
									<el-input v-model="form.deptno" autocoplete="off" type="hidden"></el-input>
								</el-form-item>
								<el-button  type="primary" @click="sureAdd" :disabled="disable3">确 定</el-button>
							</el-form>
						</el-dialog>
						<el-dialog title="给员工添加权限" :visible.sync="dialogFormVisible2">
							<el-form :model="form2" label-width="100px">
								<el-input v-model="form2.rid" autocoplete="off" type="hidden"></el-input>
								<el-form-item label="权限名称:">
									<el-input v-model="form2.rname" autocoplete="off" disabled></el-input>
								</el-form-item>								
								<el-form-item label="员工姓名:">
									<el-select v-model="pickemp" placeholder="请选择" style="float:left" @change="checkpick()">
										<el-option
											v-for="item in emps"
											:key="item.eid"
											:label="item.ename"
											:value="item.eid">
										</el-option>
									</el-select>
								</el-form-item>
								<el-input v-model="form2.deptno" autocoplete="off" type="hidden"></el-input>
								<el-button type="primary" @click="sureAddEMP" :disabled="disable">确 定</el-button>
							</el-form>
						</el-dialog>
						<div slot="footer" class="dialog-footer">
							<el-button @click="dialogFormVisible = false">取 消</el-button>
						</div>
					</el-tab-pane>
					<el-tab-pane label="删除权限" name="1">
							<h3>删除权限</h3></br></br>
							<el-form  label-width="100px" style="height:200px;">
								<el-select v-model="pickright" placeholder="请选择" @change="checkpick2()">
									<el-option
										v-for="item in rights"
										v-if="item.num==0"
										:key="item.rid"
										:label="item.rname"
										:value="item.rid">
									</el-option>
								</el-select>
								<el-button type="primary" @click="delright" :disabled="disable2">确 定</el-button>
							</el-form>
					</el-tab-pane>
				</el-tabs>
			</el-row>
		</div>
</template>

<script>
	export default {
		inject: ['reload'],
		name:'querymanager',
		data() {
			return {
				me:this.$parent.$parent.$parent.$parent.emp,
				rights:[],
				emps:[],
				disable:true,
				disable2:true,
				disable3:true,
				pickright:'',
				pickemp:'',
				myindex:-1,
				dialogFormVisible:false,
				dialogFormVisible2:false,
				TabVisible:true,
				form:{
						rid:'',
						rname:'',
						deptno:'',
						num:0,
					},
				form2:{
						rid:'',
						rname:'',
						deptno:'',
						num:'',
					},
				isedit:false,
				myindex:-1,
				powerid:0,
				error:'',
				activeName:0,
			};
		},
		created(){
			this.$axios.post("shright?deptno="+this.me.deptno+"").then(v=>{
				console.log(v)
				if(v.data){
					this.rights=v.data;
				}else{
					alert("出错了！！");
				}
			}).catch(ex=>{
				alert("出错了！"+ex);
			})
		},
		methods:{
			
			add(){
				this.dialogFormVisible=true;
			},
			sureAdd(){
				//确认添加
				
				this.dialogFormVisible=false;
				this.form.deptno=this.me.deptno;
				this.form.rid=this.rights[this.rights.length-1].rid+1;
				this.$axios.post("addright",this.form).then(v=>{
					console.log(v)
					if(v.data){
						this.$message.success("添加成功！");
						//this.reload();
					}else{
						alert("出错了！！");
					}
				}).catch(ex=>{
					alert("出错了！"+ex);
				})
				this.rights.push(this.form);
				this.form={
					rid:'',
					rname:'',
					deptno:'',
					num:0,
				}
				
					
			},
			addemp(index,row){
				this.dialogFormVisible2=true;
				this.$axios.post("sempnoright?deptno="+row.deptno+"&rid="+row.rid+"").then(v=>{
					console.log(v)
					if(v.data){
						this.emps=v.data;
					}else{
						alert("出错了！！");
					}
				}).catch(ex=>{
					alert("出错了！"+ex);
				})
				this.form2={
					rid:row.rid,
					rname:row.rname,
					deptno:row.deptno,
					num:row.num+1,
					}
					this.myindex=index;
				
			},
			sureAddEMP(){
				//确认添加
				this.dialogFormVisible2=false;
				this.rights.splice(this.myindex,1,this.form2);
				
				this.$axios.post("empaddrt?eid="+this.pickemp+"&rid="+this.form2.rid+"").then(v=>{
					console.log(v)
					if(v.data){
						alert("添加权限成功！！");
						this.dialogFormVisible2=false;
					}else{
						alert("出错了！！");
					}
				}).catch(ex=>{
					alert("出错了！"+ex);
				})
				this.pickemp="";
				this.form2={
					rid:'',
					rname:'',
					deptno:'',
					num:'',
					}
			},
			
			checkpick(){
				if(this.pickemp==''){
					this.disable=true;
				}else{
					this.disable=false;
					}
			},
			checkpick2(){
				if(this.pickright==''){
					this.disable2=true;
				}else{
					this.disable2=false;
					}
			},
			checkpick3(){
				if(this.form.dname==''){
					this.disable3=true;
				}else{
					this.disable3=false;
					}
			},
			delright(){
				
				this.$axios.post("delright?rid="+this.pickright+"").then(v=>{
					console.log(v)
					if(v.data){
						this.$message.success("删除成功！");
					}else{
						//alert("出错了！！");
						this.$message.error("出错了")
					}
				}).catch(ex=>{
					//alert("出错了！"+ex);
					this.$message.error("出错了")
				})
				
				for(var i=0;i<this.rights.length;i++){
					if(this.rights[i].rid==this.pickright){
						this.rights.splice(i,1);
						break;
					}
				} 
				this.pickright='';
				this.activeName="0";
			},
			
		}
	}
</script>

<style>
.rolebox{
	border:1px solid white;
	}
/* .rolebox .el-tabs__item{
	display: block;
} */
.el-tab-pane tr th{
	height:60px;
}
 .el-tabs__nav .el-tabs__item:nth-child(1) span{
    display: none;
} 
</style>
